<template>
  <view>
    <!--		<van-field v-model="queryObj.title" label="标题" placeholder="请输入" input-align="right" />-->
    <van-cell is-link title="公文类型" v-model="queryObj.bizTypeName" @click="showDocType = true" />
    <van-action-sheet round v-model="showDocType" :actions="docTypeList" cancel-text="取消" @select="onSelectDocType" />

    <!--		<van-field v-model="queryObj.cbrName" label="呈报人" is-link input-align="right" />-->
    <van-cell title="呈报人" :value="queryObj.cbrName"    @click="showDrafterSheet"  is-link/>
    <drafter :showDrafter="showDrafter" :drafterData="drafterData"
             @closeDrafterSheet="closeDrafterSheet"></drafter>

    <!--		<van-field v-model="queryObj.cbdeptName" label="呈报单位" is-link input-align="right" />-->
    <van-cell  title="呈报单位" :value="queryObj.cbdeptName"    @click="showOrgtreeSheet"  is-link/>
    <orgtree :showOrgtree="showOrgtree" @closeOrgTreeSheet="closeOrgTreeSheet"></orgtree>

    <van-cell is-link title="个性分类" v-model="queryObj.personalClassificationName" @click="showPersonalClassification = true" />
    <van-action-sheet round v-model="showPersonalClassification" :actions="personalClassificationList" cancel-text="取消" @select="onSelectPersonalClassification" />

    <van-field v-model="queryObj.taskNumber" label="外事任务编号" placeholder="请输入" input-align="right" />

    <van-cell title="起始时间" is-link :value="queryObj.date" @click="showdateRange = true" />
    <van-calendar v-model="showdateRange" type="range" :min-date="minDate" :max-date="maxDate"
                  @confirm="onConfirmDate" color="#1989fa" />

    <van-cell title="排序" is-link :value="queryObj.orderNameName" @click="showOrderName = true" />
    <van-action-sheet round v-model="showOrderName" :actions="showOrderNameList" cancel-text="取消" @select="onSelectShowOrderName" />

    <view class="padding-top-lg padding-bottom-lg">
      <van-row type="flex" justify="space-around">
        <van-col span="10">
          <van-button type="info" round plain hairline block @click="toClose">重置</van-button>
        </van-col>
        <van-col span="10">
          <van-button type="info" round block @click="onConfirm">确定</van-button>
        </van-col>
      </van-row>
    </view>
  </view>
</template>

<script>
import drafter from 'pages/caac/components/query/compo-drafter';
import orgtree from 'pages/caac/components/query/compo-orgtree';
//拟稿人选择
export default {
  components: {
    drafter,orgtree
  },
  props: {
    title: {
      type: String,
      default: ''
    },
  },
  watch: {
    title:{
      handler(nVal, oVal) {
        if(this.util.isNotEmpty(nVal)){
          this.queryObj.title = nVal
        }
      },
      immediate: true,
      deep: true
    }
  },
  data() {
    return {
      queryObj:{
        title: '',//标题
        bizTypeCode: '',//公文类型code
        bizTypeName:'请选择',//公文类型名称
        cbr: '',//呈报人code
        cbrName:'',//呈报人name
        cbdept: '',//呈报单位code
        cbdeptName: '',//呈报单位name
        personalClassification:'',//个性分类code
        personalClassificationName:'',//个性分类name
        taskNumber:'',//外事任务编号
        date: '',
        createTimeStart:'',
        createTimeEnd:'',
        orderName:'completeTime',//排序completeTime结束时间,createTime提交时间,code公文类型,cbdept单位
        orderNameName:'结束时间'
      },
      personalClassificationList:[],//个性分类
      showPersonalClassification:false,
      showdateRange: false,

      showDocType: false,
      docTypeList: [],
      showOrderName:false,
      showOrderNameList:[],
      showDrafter:false,//呈报人组件
      showOrgtree:false,//呈报单位
      //拟稿人信息，串回子组件回显用
      drafterData:{
        userName:'',
        userId:''
      },
    };
  },

  computed: {
    minDate: function () {
      let d = new Date()
      let y = d.getFullYear() - 10
      return new Date(y, 0, 1)
    },
    maxDate: function () {
      let d = new Date()
      let y = d.getFullYear() + 10
      return new Date(y, 0, 1)
    }
  },

  mounted() {
    //获取个性分类字典
    this.getGxfl()
    //获取公文类型-管理局：002001_configCode ,民航局：001_configCode，监管局002001006001_configCode
    this.getDictDocType('001_configCode')
    //排序字段赋值
    this.showOrderNameList.push(
        {name:'结束时间',value:'completeTime'},
        {name:'提交时间',value:'createTime'},
        {name:'公文类型',value:'code'},
        {name:'单位',value:'cbdept'}
    )
  },
  methods: {
    //呈报人
    showDrafterSheet(){
      this.showDrafter = true
    },
    //关闭呈报人组件
    closeDrafterSheet(userInfo){
      //userInfo: {"userId":"4926","userName":"高萌"}
      this.queryObj.cbr= userInfo.userId
      this.queryObj.cbrName = userInfo.userName
      this.showDrafter = false
    },
    //呈报单位
    showOrgtreeSheet(){
      this.showOrgtree = true
    },
    //关闭呈报单位
    closeOrgTreeSheet(orgInfo){
      //orgInfo = {orgName:this.deptName,deptCode:this.deptCode}
      this.queryObj.cbdept = orgInfo.deptCode
      this.queryObj.cbdeptName = orgInfo.orgName
      this.showOrgtree = false
    },
    //个性分类
    getGxfl(){
      this.personalClassificationList=[]
      // this.personalClassificationList.push({name:'请选择',value:''})
      let gxflArr = this.cache.get('gxfl')
      gxflArr.forEach((item)=>{
        this.personalClassificationList.push({name:item.dictItemName,value:item.dictItemCode})
      })
      if(this.personalClassificationList.length>0){
        this.queryObj.personalClassification = this.personalClassificationList[0].value
        this.queryObj.personalClassificationName = this.personalClassificationList[0].name
      }
    },
    //公文类型
    getDictDocType(dictCode){
      this.docTypeList=[]//清空
      this.docTypeList.push({name:'全部',value:''})
      //002001_configCode
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/comm/dict/dicts`),
        method: 'get',
        params: this.$http.adornParams({
          'dictCodes': dictCode,
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag//固定值
        })
      }).then(({data}) => {
        if(data.code===200){
          data.data[dictCode].forEach((item)=>{
            this.docTypeList.push({name:item.dictItemName,value:item.dictItemCode})
          })
        }else{
          console.log('获取字典信息失败,code:'+data.code+',message:'+data.message);
        }
      })
    },
    //公文类型选择
    onSelectDocType(item) {
      this.queryObj.bizTypeName = item.name
      this.queryObj.bizTypeCode = item.value
      this.showDocType = false
    },
    //个性化分类选择
    onSelectPersonalClassification(item){
      this.queryObj.personalClassification = item.value
      this.queryObj.personalClassificationName = item.name
      this.showPersonalClassification = false
    },
    //排序
    onSelectShowOrderName(item){
      this.queryObj.orderName = item.value
      this.queryObj.orderNameName = item.name
      this.showOrderName = false
    },
    formatDate(date) {
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
    },
    onConfirmDate(date) {
      const [start, end] = date;
      this.showdateRange = false;
      this.queryObj.createTimeStart = this.formatDate(start)
      this.queryObj.createTimeEnd = this.formatDate(end)
      this.queryObj.date = `${this.formatDate(start)} - ${this.formatDate(end)}`;
    },
    onConfirm() {
      this.$emit('filterToggle',this.queryObj);
    },
    //重置按钮
    toClose(){
      //重置
      this.queryObj={
        title: '',//标题
        bizTypeCode: '',//公文类型code
        bizTypeName:'全部',//公文类型名称
        cbr: '',//呈报人code
        cbrName:'',//呈报人name
        cbdept: '',//呈报单位code
        cbdeptName: '',//呈报单位name
        personalClassification:'',//个性分类code
        personalClassificationName:'',//个性分类name
        taskNumber:'',//外事任务编号
        date: '',
        createTimeStart:'',
        createTimeEnd:'',
        orderName:'completeTime',//排序completeTime结束时间,createTime提交时间,code公文类型,cbdept单位
        orderNameName:'结束时间'
      }
      if(this.personalClassificationList.length>0){
        this.queryObj.personalClassification = this.personalClassificationList[0].value
        this.queryObj.personalClassificationName = this.personalClassificationList[0].name
      }
      this.$emit('filterToggle',this.queryObj);
    }

  },
};
</script>

<style scoped>
.van-cell__title {
  min-width: 50%;

span {
  display: inline-block;
  text-align: left;
  word-break: break-all;
}
}

/deep/ .van-field__label {
  color: #323233;
  min-width: 30%;

span {
  display: inline-block;
  text-align: left;
  word-break: break-all;
}
}

/deep/ .van-field__control {
  font-size: 14px;
  color: #969799;
}
</style>
